package cn.com.wangluotongxin.linux.docker;

import cn.hutool.core.util.StrUtil;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.model.Frame;
import lombok.extern.slf4j.Slf4j;

import java.io.Closeable;
import java.io.IOException;

@Slf4j
public abstract class LogResultCallback implements ResultCallback {

    @Override
    public void onStart(Closeable closeable) {
        log.info("开始加载docker镜像日志");
    }

    @Override
    public void onNext(Object object) {
        Frame frame = (Frame) object;
        String log = new String(frame.getPayload());
        if(StrUtil.isBlank(log)) return;
        this.outLog(log);
    }

    @Override
    public void onError(Throwable throwable) {
        log.info("加载docker镜像日志异常:{}", throwable.toString());
    }

    @Override
    public void onComplete() {
        log.info("加载docker镜像日志完毕");
    }

    @Override
    public void close() throws IOException {
        log.info("关闭加载docker镜像日志");
    }

    abstract void outLog(String log);
}
